#ifndef COMMUNICATION_H
#define COMMUNICATION_H

#include <stdint.h>
#include <sys/types.h>

#define LEN_PKG	256

//------------------------------------------------------------------------
// 遥控包格式
/*
B00-03		ID-H
B04-07		ID-L
B08-11		TYPE:0x00000001-SGAGV
B12-15		APID:0x80000001-TC
B16-19		PackageLen(B0-Bx总字节数):256B
B20-23		Time-H(UTC)
B24-27		Time-L(UTC)
B28-31		Reserve
---
B32-35		CMD
B36-39		v1
B40-43		v2
B44-47		v3
B48-51		
B52-55
...
*/
typedef struct SGAGV_TM
{
	uint32_t	IDH;
	uint32_t	IDL;
	uint32_t	type;
	uint32_t	aipd;
	uint32_t	pkgLen;
	uint32_t	timeH;
	uint32_t	timeL;
	uint32_t	reserve;
	//---
	uint32_t	cmdid;
	uint32_t	v[5];

}SGAGV_TM;


//接收的包
typedef struct SGAGV_TC
{
	uint32_t	IDH;
	uint32_t	IDL;
	uint32_t	type;
	uint32_t	aipd;
	uint32_t	pkgLen;
	uint32_t	timeH;
	uint32_t	timeL;
	uint32_t	reserve;
	//---
	uint32_t	gpsX;
	uint32_t	gpsZ;
	uint32_t	angle;
	uint32_t	speed;
	uint32_t   leftds;
    uint32_t   middleds;
    uint32_t   rightds;
	uint32_t   cameraX; //摄像头识别物体相对摄像头的x值
    uint32_t   cameraY;
    uint32_t   cameraZ;
	uint32_t 	force;//力传感器
	uint32_t	currCmd;
}SGAGV_TC;

int connectToServer(char * IP,char * port);
void buildTM(char buffer[],uint32_t socket_port,uint32_t Cmd,uint32_t *argv,int argc);
ssize_t readn(int fd,void *ptr,size_t n);
ssize_t writen(int fd,void *ptr,size_t n);
void readCom(int sockid);
void sendCom(int sockid,uint32_t port,uint32_t cmd,uint32_t*argv,int argc);
SGAGV_TC parseCmd(char buffer[]);
void oops(char *s,int num);
#endif // !COMMUNICATION_H
